package com.efounder.frame.activity.helper;

import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;

import com.core.xml.StubObject;
import com.efounder.frame.activity.manager.EFAppAccountActivityFragmentManager;
import com.efounder.frame.widget.EFPopupMenu;
import com.efounder.frame.widget.EFSlidingTabStrip;
import com.efounder.frame.xmlparse.EFAppAccountRegistry;
import com.efounder.frame.xmlparse.EFXmlConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hudq on 2016/8/29. xx
 */

public class EFAppAccountActivityHelper {
    private FragmentActivity activity;
    private EFAppAccountActivityFragmentManager appAccountActivityFragmentManager;
    private EFPopupMenu popupMenu;
    /**此position需要做回被收后，恢复数据的处理:onSaveInstanceState**/
    private int currentPosition = 0;

    private EFSlidingTabStrip slidingTabStrip;

    public EFAppAccountActivityHelper(FragmentActivity activity, EFSlidingTabStrip slidingTabStrip){
        super();
        this.activity = activity;
        appAccountActivityFragmentManager = new EFAppAccountActivityFragmentManager(activity.getSupportFragmentManager());
        this.slidingTabStrip = slidingTabStrip;
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
        appAccountActivityFragmentManager.setCurrentPosition(currentPosition);
        slidingTabStrip.setCurrentPosition(currentPosition);
    }

    public void createSlidingTabStrip(final List<StubObject> oneLevelStubList, Bundle savedInstanceState){
        List<String> titleList = new ArrayList<>();
        for (StubObject oneLevelStub: oneLevelStubList ){
            titleList.add(oneLevelStub.getCaption());
        }
        titleList.add("消息");
        slidingTabStrip.setTitleList(titleList);
        slidingTabStrip.setCurrentPosition(currentPosition);
        if (savedInstanceState == null){//回收重建，fragment会自动重建
            showFragment(currentPosition,oneLevelStubList.get(currentPosition));
        }
        slidingTabStrip.setOnItemClickListener(new EFSlidingTabStrip.OnItemClickListener() {
            @Override
            public void onItemClick(EFSlidingTabStrip slidingTabStrip, View view, int position, String title) {
                currentPosition = position;
                if (position < oneLevelStubList.size()){
                    showFragment(currentPosition,oneLevelStubList.get(currentPosition));
                }else {
                    //显示消息Fragment
                    showFragment(currentPosition,null);
                }
            }
        });
    }

    public void createPopupMenu(View anchor, List<StubObject> menuStubList){
        if (popupMenu == null){
            popupMenu = new EFPopupMenu(anchor.getContext());
            EFPopupMenu.PopupMenuItemModel itemModel1 = popupMenu.new PopupMenuItemModel();
            itemModel1.setText("关于");
            popupMenu.addItem(itemModel1);
            EFPopupMenu.PopupMenuItemModel itemModel2 = popupMenu.new PopupMenuItemModel();
            itemModel2.setText("帮助");
            popupMenu.addItem(itemModel2);
            EFPopupMenu.PopupMenuItemModel itemModel3 = popupMenu.new PopupMenuItemModel();
            itemModel3.setText("退出");
            popupMenu.addItem(itemModel3);
            for (StubObject menuStub:menuStubList) {
                EFPopupMenu.PopupMenuItemModel itemModel = popupMenu.new PopupMenuItemModel();
                itemModel.setText(menuStub.getCaption());
                popupMenu.addItem(itemModel);
            }
            popupMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    popupMenu.dismiss();
                    //TODO 点击菜单按钮
                    Object object = parent.getItemAtPosition(position);
                    if (object instanceof EFPopupMenu.PopupMenuItemModel){
                        EFPopupMenu.PopupMenuItemModel itemModel = (EFPopupMenu.PopupMenuItemModel) object;
                        String text = itemModel.getText();
                        if ("关于".equals(text)){

                        }else if ("帮助".equals(text)){

                        }else if ("退出".equals(text)){
                            activity.finish();
                        }
                    }
                }
            });
        }
        if (popupMenu.isShowing()){
            popupMenu.dismiss();
        }else {
            popupMenu.show(anchor);
        }

    }

    private void showFragment(int targetPosition, StubObject oneLevelStub){
        List<StubObject> twoLevelStubList = null;
        if (oneLevelStub != null){
            twoLevelStubList = EFAppAccountRegistry.getStubListByElementName(oneLevelStub.getString(EFXmlConstants.ATTR_ID,""));
        }
        appAccountActivityFragmentManager.showFragment(targetPosition,twoLevelStubList,false);
    }


}
